package com.stylefeng.guns.modular.cash.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.cash.model.PScash;
import com.stylefeng.guns.modular.cash.service.ICashExpenditureService;
import com.stylefeng.guns.modular.cash.service.IDetailsService;
import com.stylefeng.guns.modular.system.dao.CashExpenditureMapper;
import com.stylefeng.guns.modular.system.model.CashExpenditure;
import com.stylefeng.guns.modular.system.model.Details;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  piso现金收支服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-07-03
 */
@Service
public class CashExpenditureServiceImpl extends ServiceImpl<CashExpenditureMapper, CashExpenditure> implements ICashExpenditureService {

    @Resource
    private CashExpenditureMapper cashExpenditureMapper;

    @Autowired
    private IDetailsService detailsService;



    /**
     * 新增现金数据
     * @param cashExpenditure
     * @return
     * @author laoyu
     * @date 2018-07-24
     */
    @Override
    public boolean insertCash(CashExpenditure cashExpenditure) {

        //查询最后一笔现金收支数据 获取余额
        CashExpenditure cashExpenditure1 = cashExpenditureMapper.searchLast(cashExpenditure.getPlatform());
        if(cashExpenditure1!=null ){
            if (cashExpenditure.getType()==CashExpenditure.Enum_Type.收入.value){
                cashExpenditure.setBalance(cashExpenditure1.getBalance()+cashExpenditure.getMoney());
            }
            if (cashExpenditure.getType()==CashExpenditure.Enum_Type.支出.value){
                cashExpenditure.setBalance(cashExpenditure1.getBalance()-cashExpenditure.getMoney());
            }
        }else {
            cashExpenditure.setBalance(cashExpenditure.getMoney());
        }

        return this.insert(cashExpenditure);

    }

    /**
     * 描述:组装piso现金收支报表页面数据
     * 创建人: laoyu
     * 日期: 2018/7/30 0030
     * 时间: 下午 4:42
     */
    @Override
    public Page<CashExpenditure> serach(Page<CashExpenditure> page, EntityWrapper<CashExpenditure> wrapper) throws GunsException {
        Page<CashExpenditure> cashExpenditurePage=this.selectPage(page,wrapper);

        List<CashExpenditure> cashExpenditureList = cashExpenditurePage.getRecords();
        if (cashExpenditureList == null||cashExpenditureList.isEmpty()){
            return new Page<CashExpenditure>();
        }
        List<CashExpenditure> cashExpenditures = new ArrayList<>();
        for (CashExpenditure cashExpenditure : cashExpenditureList) {
            Details details=detailsService.selectById(cashExpenditure.getDetailsId());
            if (details!=null && (details.getType() == CashExpenditure.Enum_DetailType.生活成本费用.value)) {

                cashExpenditure.setPayoutTatol(cashExpenditure.getMoney());
                cashExpenditures.add(cashExpenditure);
            }else {
                cashExpenditures.add(cashExpenditure);
            }
        }
        cashExpenditurePage.setRecords(cashExpenditures);
        return cashExpenditurePage;
    }

    /**
     * 描述:修改现金收支表余额
     * 创建人: laoyu
     * 日期: 2018/7/30 0030
     * 时间: 下午 9:51
     */
    @Override
    public void update(CashExpenditure oldCashExpenditure,CashExpenditure cashExpenditure) throws GunsException {

        double difference = 0;//修改差额
        if (oldCashExpenditure.getType()==cashExpenditure.getType()){//收支类型相同
            if (cashExpenditure.getType()==CashExpenditure.Enum_Type.收入.value){
                difference = cashExpenditure.getMoney()-oldCashExpenditure.getMoney();
            }else if (cashExpenditure.getType() == CashExpenditure.Enum_Type.支出.value){
                difference = oldCashExpenditure.getMoney()-cashExpenditure.getMoney();
            }
        }else {
            if (cashExpenditure.getType() == CashExpenditure.Enum_Type.收入.value)
                difference = cashExpenditure.getMoney()+oldCashExpenditure.getMoney();
            if (cashExpenditure.getType() == CashExpenditure.Enum_Type.支出.value)
                difference = -(cashExpenditure.getMoney()+oldCashExpenditure.getMoney());

        }
        baseMapper.updateBalance(oldCashExpenditure.getId(),ShiroKit.getUser().getDeptcode(),difference);


    }

    /**
     * 描述:删除报表后修改余额
     * 创建人: laoyu
     * 日期: 2018/8/9 0009
     * 时间: 下午 5:00
     */
    @Override
    public void updateBalance(int id, CashExpenditure cashExpenditure) throws GunsException {

        double difference = 0;//修改差额

            if (cashExpenditure.getType() == CashExpenditure.Enum_Type.收入.value)
                difference = cashExpenditure.getMoney();
            if (cashExpenditure.getType() == CashExpenditure.Enum_Type.支出.value)
                difference = -cashExpenditure.getMoney();

        baseMapper.updateBalance(id,ShiroKit.getUser().getDeptcode(),-difference);

    }

    /**
     * 描述:组装下载数据
     * 创建人: laoyu
     * 日期: 2018/8/2 0002
     * 时间: 下午 5:43
     */
    @Override
    public Map<String, Object> load(List<CashExpenditure> cashExpenditureList,String platform,String beginDate,String endDate) throws GunsException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Map<String,Object> cashMap = new HashMap<>();
        List<PScash> cashList = new ArrayList();

        double incomeTotal = 0;//收入总和
        double outTotal = 0;//支出总和
        double payrollTotal = 0;//工资总和
        double foodTotal = 0;//食堂几餐不总和
        double rentTotal = 0;//房租总和
        double waterTotal = 0;//水费总和
        double elecTotal = 0;//电费总和
        double netTotal = 0;//网费总和
        double ticketTotal = 0;//机票费用总和
        double visaTotal = 0;//签证保关总和
        double officeTotal = 0;//办公费用总和
        double otherTotal = 0;//其他总和
        double total = 0;//所有总和


        for (CashExpenditure cashExpenditure : cashExpenditureList){
            PScash pScash = new PScash();
            pScash.setRevDate(simpleDateFormat.format(cashExpenditure.getRevDate()));
            if (cashExpenditure.getType()==CashExpenditure.Enum_Type.收入.value) {
                pScash.setIncome(cashExpenditure.getMoney());
                incomeTotal += cashExpenditure.getMoney();
            }
            else if (cashExpenditure.getType() == CashExpenditure.Enum_Type.支出.value) {
                pScash.setPayout(cashExpenditure.getMoney());
                outTotal += cashExpenditure.getMoney();
            }

            if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.工资薪酬.value) {
                pScash.setPayroll(cashExpenditure.getMoney());
                payrollTotal += cashExpenditure.getMoney();
                total += cashExpenditure.getMoney();
                pScash.setSubtotal(cashExpenditure.getMoney());
            }
            else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.食堂及补贴.value) {
                pScash.setFood(cashExpenditure.getMoney());
                foodTotal += cashExpenditure.getMoney();
                total += cashExpenditure.getMoney();
                pScash.setSubtotal(cashExpenditure.getMoney());
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.房租费用.value) {
                rentTotal += cashExpenditure.getMoney();
                pScash.setRent(cashExpenditure.getMoney());
                total += cashExpenditure.getMoney();
                pScash.setSubtotal(cashExpenditure.getMoney());
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.水费.value) {
                waterTotal += cashExpenditure.getMoney();
                pScash.setWater(cashExpenditure.getMoney());
                total += cashExpenditure.getMoney();
                pScash.setSubtotal(cashExpenditure.getMoney());
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.电费.value) {
                elecTotal += cashExpenditure.getMoney();
                pScash.setElectricity(cashExpenditure.getMoney());
                total += cashExpenditure.getMoney();
                pScash.setSubtotal(cashExpenditure.getMoney());
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.网络费用.value) {
                netTotal += cashExpenditure.getMoney();
                pScash.setNet(cashExpenditure.getMoney());
                pScash.setSubtotal(cashExpenditure.getMoney());
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.机票费用.value) {
                ticketTotal += cashExpenditure.getMoney();
                pScash.setTickets(cashExpenditure.getMoney());
                total += cashExpenditure.getMoney();
                pScash.setSubtotal(cashExpenditure.getMoney());
            }
            else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.签证及保关.value) {
                visaTotal += cashExpenditure.getMoney();
                pScash.setVisa(cashExpenditure.getMoney());
                total += cashExpenditure.getMoney();
                pScash.setSubtotal(cashExpenditure.getMoney());
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.办公费用.value) {
                officeTotal += cashExpenditure.getMoney();
                pScash.setOffice(cashExpenditure.getMoney());
                total += cashExpenditure.getMoney();
                pScash.setSubtotal(cashExpenditure.getMoney());
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.其他杂费.value) {
                otherTotal += cashExpenditure.getMoney();
                pScash.setOther(cashExpenditure.getMoney());
                total += cashExpenditure.getMoney();
                pScash.setSubtotal(cashExpenditure.getMoney());
            }
            pScash.setBalance(cashExpenditure.getBalance());
            cashList.add(pScash);
        }

        cashMap.put("cashList",cashList);
        cashMap.put("incomeTotal",incomeTotal);
        cashMap.put("outTotal",outTotal);
        cashMap.put("payrollTotal",payrollTotal);
        cashMap.put("foodTotal",foodTotal);
        cashMap.put("rentTotal",rentTotal);
        cashMap.put("waterTotal",waterTotal);
        cashMap.put("elecTotal",elecTotal);
        cashMap.put("netTotal",netTotal);
        cashMap.put("ticketTotal",ticketTotal);
        cashMap.put("visaTotal",visaTotal);
        cashMap.put("officeTotal",officeTotal);
        cashMap.put("otherTotal",otherTotal);
        cashMap.put("total",total);
        cashMap.put("name",platform.toUpperCase()+beginDate+"至"+endDate+"PS收入/支出明细分类表");

        return cashMap;
    }

    /**
     * 描述:PS收入/支出明细分类表
     * 创建人: laoyu
     * 日期: 2018/8/8 0008
     * 时间: 下午 4:40
     */
    @Override
    public Map<String, Object> total(EntityWrapper<CashExpenditure> wrapper) throws GunsException {

        List<CashExpenditure> cashExpenditureList = this.selectList(wrapper);
        double incomeTotal=0;//收入金额
        double outTotal=0;//支出金额
        double payrollTotal=0;//工资金额
        double foodTotal=0;//食堂及餐补金额
        double rentTotal=0;//房租金额
        double waterTotal=0;//水费金额
        double elecTotal=0;//电费金额
        double netTotal=0;//网络金额
        double ticketTotal=0;//机票金额
        double visaTotal=0;//签证金额
        double officeTotal=0;//办公金额
        double otherTotal=0;//其他金额
        double allTotal=0;//小计金额

        for (CashExpenditure cashExpenditure : cashExpenditureList){
            if (cashExpenditure.getType()==CashExpenditure.Enum_Type.收入.value) {
                incomeTotal += cashExpenditure.getMoney();
            }
            else if (cashExpenditure.getType() == CashExpenditure.Enum_Type.支出.value) {
                outTotal += cashExpenditure.getMoney();
            }

            if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.工资薪酬.value) {
                payrollTotal += cashExpenditure.getMoney();
                allTotal += cashExpenditure.getMoney();
            }
            else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.食堂及补贴.value) {
                foodTotal += cashExpenditure.getMoney();
                allTotal += cashExpenditure.getMoney();
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.房租费用.value) {
                rentTotal += cashExpenditure.getMoney();
                allTotal += cashExpenditure.getMoney();
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.水费.value) {
                waterTotal += cashExpenditure.getMoney();
                allTotal += cashExpenditure.getMoney();
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.电费.value) {
                elecTotal += cashExpenditure.getMoney();
                allTotal += cashExpenditure.getMoney();
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.网络费用.value) {
                netTotal += cashExpenditure.getMoney();
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.机票费用.value) {
                ticketTotal += cashExpenditure.getMoney();
                allTotal += cashExpenditure.getMoney();
            }
            else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.签证及保关.value) {
                visaTotal += cashExpenditure.getMoney();
                allTotal += cashExpenditure.getMoney();
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.办公费用.value) {
                officeTotal += cashExpenditure.getMoney();
                allTotal += cashExpenditure.getMoney();
            }
            else if (cashExpenditure.getDetailsId() == CashExpenditure.Enum_DetailTypes.其他杂费.value) {
                otherTotal += cashExpenditure.getMoney();
                allTotal += cashExpenditure.getMoney();
            }
        }

        Map<String,Object> cashMap = new HashMap<>();
        cashMap.put("incomeTotal",incomeTotal);
        cashMap.put("outTotal",outTotal);
        cashMap.put("payrollTotal",payrollTotal);
        cashMap.put("foodTotal",foodTotal);
        cashMap.put("rentTotal",rentTotal);
        cashMap.put("waterTotal",waterTotal);
        cashMap.put("elecTotal",elecTotal);
        cashMap.put("netTotal",netTotal);
        cashMap.put("ticketTotal",ticketTotal);
        cashMap.put("visaTotal",visaTotal);
        cashMap.put("officeTotal",officeTotal);
        cashMap.put("otherTotal",otherTotal);
        cashMap.put("allTotal",allTotal);
        return cashMap;
    }
}
